<template>
  <div class="main">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
    <el-form-item label="昵称" prop="nickname">
        <el-input v-model.number="ruleForm.nickname"></el-input>
      </el-form-item>
    <el-form-item label="账号" prop="account" v-if="!iflogin">
        <el-input v-model.number="ruleForm.account"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass" v-if="!iflogin">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm','register')" v-if="!iflogin">注册</el-button>
        <el-button type="primary" @click="submitForm('ruleForm','login')" v-if="iflogin">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click.native.prevent="quehuan">{{iflogin?'注册新账号':'已有账号？登录'}}</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import requests from '@/https.js'

export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') { 
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      var Vnickname =(rule,value,callback)=>{
          if(value===''){
              callback(new Error('请输入昵称'))
          }else {
          callback();
        }
      };
       var Vaccount =(rule,value,callback)=>{
          if(value===''){
              callback(new Error('请输入账号'))
          }else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '123456',
          checkPass: '123456',
          nickname:'hua',
          account:'hua001',
          iflogin:false,
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          nickname:[
              { validator: Vnickname, trigger: 'blur' }
          ],
           account:[
              { validator: Vaccount, trigger: 'blur' }
          ]
        }
      };
    },
    created(){
      if (localStorage.Authorization !== "") {  // 通过vuex state获取当前的token是否存
            this.iflogin = true
      }
    },
    methods: {
      quehuan(){
          this.iflogin = !this.iflogin
          this.$forceUpdate()
          console.log(this.iflogin)
      },
      test(){
                    // console.log(localStorage.Authorization)
                    console.log(this.$store.state)
                    this.$store.commit('CHANGELOGIN','abc')
                    // console.log(localStorage.Authorization)
          
      },
        // mutations(['changeLogin']),
      submitForm(formName,key) {
        //  storage.removeItem(Authorization);
        this.$refs[formName].validate((valid) => {
          if (valid) {
              console.log(valid)
              let from = {'key':key,data:{'nickname':this.ruleForm.nickname,'account':this.ruleForm.account,'pass': this.ruleForm.pass}};
              console.log(from)
              requests.post('/api/account',from).then(res=>{
                  if(res.data.code ==0){
                    // 保存用户名
                    localStorage.setItem('nickName', res.data.data.nickname);
                    let userToken = 'Bearer ' + res.data.data.token;
                    // 将用户token保存到vuex中
                    this.$store.commit('CHANGELOGIN',{ 'Authorization':userToken,'nickName':res.data.data.nickname})
                         this.$notify({
                          title: '登录成功',
                          type: 'success'
                        });

                    this.$router.push('/');
                  }else{
                    
                  }
                  
              })
          }else{
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
};
</script>

<style scoped>
    .main{
        margin-top: 10%;
        display: flex;
        justify-content: center;
        align-items: center;
    }
</style>